package org.byqh.dqeai.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.byqh.dqeai.common.Json;
import org.byqh.dqeai.dto.SysAppDTO;
import org.byqh.dqeai.dto.common.IPage;
import org.byqh.dqeai.entity.SysApp;
import org.byqh.dqeai.entity.proxy.SysAppProxy;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/app")
@Tag(name = "系统[SysApp] - 应用管理")
public class SysAppController extends BaseController<SysApp, SysAppProxy> {

    @Override
    @GetMapping(value = "/get/{id}")
    public Json<SysApp> get(String id) {
        return super.get(id);
    }

    @Override
    @PutMapping(value = "/put")
    public Json<Boolean> put(SysApp dto) {
        return super.put(dto);
    }

    @Override
    @PostMapping(value = "/post")
    public Json<Boolean> post(SysApp dto) {
        return super.post(dto);
    }

    @GetMapping(value = "/{id}")
    public Json<SysApp> get2(@PathVariable String id) {
        var obj = getEntityQueryable()
                .whereById(id)
                .firstOrNull();

        return Json.success(obj);
    }

    @PostMapping(value = "/")
    public Json<Boolean> post2(@RequestBody SysAppDTO dto) {
        if (StringUtils.isBlank(dto.getAppId())) {
            dto.setAppId(UUID.randomUUID().toString());
        }

        var date = LocalDateTime.now();
        dto.setCreateDate(date);
        dto.setUpdateDate(date);

        return this.save(dto);
    }

    @PutMapping(value = "/")
    public Json<Boolean> put2(@RequestBody SysAppDTO dto) {
        dto.setUpdateDate(LocalDateTime.now());
        return this.update(dto);
    }

    @DeleteMapping(value = "/{id}")
    public Json<Boolean> delete(@PathVariable String id) {
        return this.removeById(id);
    }

    @PostMapping(value = "/list")
    public Json<IPage<SysApp>> list(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer size,
                                    @RequestParam(defaultValue = "", required = false) String sort,
                                    @RequestBody(required = false) SysAppDTO dto) {

        return this.listPageImpl(SysApp.class, page, size, (query) -> {
            if (dto != null && !StringUtils.isBlank(dto.getCategoryId())) {
                query = query.where(r -> r.categoryId().eq(dto.getCategoryId()));
            }

            return query;
        });
    }

    /**
     * 查询应用对象信息列表
     */
    @PostMapping(value = "/listWithoutImage")
    public Json<IPage<SysApp>> listWithoutImage(@RequestParam(defaultValue = "1") Integer page,
                                                @RequestParam(defaultValue = "10") Integer size,
                                                @RequestParam(defaultValue = "", required = false) String sort,
                                                @RequestBody(required = false) SysAppDTO dto) {

        return this.listPageImpl(SysApp.class, page, size, (query) -> {
            if (dto != null && !StringUtils.isBlank(dto.getCategoryId())) {
                query = query.where(r -> r.categoryId().eq(dto.getCategoryId()));
            }

            return query;
        });
    }

    @PostMapping(value = "/getAppIdListByUser")
    private Json<List<String>> getAppIdListByUser(@RequestParam String usercode) {
        String sql = String.format("""
                select distinct app_id from eai_sys_role_module where role_id in (
                            select role_id from EAI_SYS_USER_ROLE where user_id = (
                                select user_id from eai_sys_user where user_code = '%s'
                            )
                        )
                """, usercode);

        var list = easyQueryClient.queryable(sql, String.class)
                .toList();

        return Json.success(list);
    }
}